package com.zwj.controller;


import com.zwj.service.AsyncService;
import com.zwj.service.UserService;
import com.zwj.vo.BaseResp;
import com.zwj.vo.entity.Report;
import com.zwj.vo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CompletableFuture;

/**
 * 用户controller
 *
 * @author zwj
 * @since 2022-05-07
 */
@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private AsyncService asyncService;

    @ApiImplicitParam(name = "name", value = "姓名", required = true)
    @ApiOperation(value = "向客人问好")
    @PostMapping("/sayHi")
    public BaseResp<String> sayHi(@RequestParam(value = "name") String name) {
        log.info("程序开始了。。。。");
        return BaseResp.ok("Hi:" + name);
    }

    @ApiOperation(value = "导出")
    @GetMapping("/userExport")
    public BaseResp<String> userExport(HttpServletResponse response) throws UnsupportedEncodingException {
        userService.userExport(response);
        return BaseResp.ok("success");
    }

    @ApiOperation(value = "测试ESB切面")
    @PostMapping("/testEsb")
    public BaseResp<User> testESB(@RequestBody User user) {
        log.error("Controller：{}", user.toString());
        User respVO = userService.testESB(user);
        log.error("Controller：{}", respVO.toString());
        return BaseResp.ok(respVO);
    }

    @ApiOperation(value = "异步生成用户报告")
    @GetMapping("/report")
    public BaseResp<String> report() {
        log.info("开始生成报告...");
        CompletableFuture<Report> future = asyncService.generateReportAsync(1L);
        future.whenComplete((report, ex) -> {
            if (ex == null) {
                log.info("生成报告成功：{}", report.getReport());
            } else {
                log.error("生成报告失败：{}", ex.getMessage());
            }
        });
        log.info("报告生成完成...");
        return BaseResp.ok("success");
    }
}

